<?php


// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 地图区域划分ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/mapAreaZoning.class.php');

class ajax_mapAreaZoning {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1: //根据城市，查询所有地图区域
                    $this->searchMapArea();
                    break;

                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array( 'del', 'edit' ))) {
                        returnJsonData('参数错误');
                    }

                    if ($_REQUEST['act'] == 'edit') {
                        $this->editMapArea();
                    } else if ($_REQUEST['act'] == 'del') {
                        $this->delMapArea();
                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    private function searchRegion() {

        global $G_SHOP;

        $region_array = array();

        //在地图区域表里查询一级地址
        $search_sql = 'SELECT oneLevelID,oneLevelName FROM sys_map_area GROUP BY oneLevelID';
        $one_level_array =& $G_SHOP->DBCA->stmt_query($search_sql);
        if (!$one_level_array) {
            returnJsonData('ok', 200);
        }

       foreach($one_level_array as &$one_level) {

           $region_array[$one_level->oneLevelID] = array(
               'id' => $one_level->oneLevelID,
               'name' => $one_level->oneLevelName,
               'list' => array()
           );

           //在地图区域表里查询二级地址
           $search_sql = 'SELECT secondLevelID,secondLevelName FROM sys_map_area WHERE oneLevelID = ? GROUP BY secondLevelID';
           $second_level_array =& $G_SHOP->DBCA->stmt_query($search_sql, 'i', $one_level->oneLevelID);
           if ($second_level_array) {

               $sec_array =& $region_array[$one_level->oneLevelID]['list'];

               foreach($second_level_array as &$second_level) {

                   $sec_array[$second_level->secondLevelID] = array(
                       'id' => $second_level->secondLevelID,
                       'name' => $second_level->secondLevelName,
                       'list' => array()
                   );

                   //在地图区域表里查询三级地址
                   $search_sql = 'SELECT threeLevelID,threeLevelName FROM sys_map_area WHERE secondLevelID = ? GROUP BY threeLevelID';
                   $three_level_array =& $G_SHOP->DBCA->stmt_query($search_sql, 'i', $second_level->secondLevelID);
                   if ($three_level_array) {

                       $three_array =& $sec_array[$second_level->secondLevelID]['list'];

                       foreach($three_level_array as $three_level) {

                           $three_array[$three_level->threeLevelID] = array(
                               'id' => $three_level->threeLevelID,
                               'name' => $three_level->threeLevelName,
                               'list' => array()
                           );

                           //在地图区域三级地址下面的区域划分
                           $search_sql = 'SELECT areaID,areaName,threeLevelID,threeLevelName FROM sys_map_area WHERE threeLevelID = ?';
                           $map_level_array =& $G_SHOP->DBCA->stmt_query($search_sql, 'i', $three_level->threeLevelID);

                           if($map_level_array){

                               $map_array =& $three_array[$three_level->threeLevelID]['list'];

                               foreach($map_level_array as $map_level) {

                                   $map_array[] = array(
                                       'id' => $map_level->threeLevelID,
                                       'areaID' => $map_level->areaID,
                                       'name' => $map_level->areaName,
                                   );
                               }
                           }
                       }
                   }
                }
            }
       }
        returnJsonData('ok', 200, $region_array);
    }

    /**
     * 保存划分的区域名称
     */
    private function editMapArea() {

        global $G_PGLOB;

        //实例化地图区域划分类
        $mapAreaZoning = new mapAreaZoning();

        //添加地图区域
        $_REQUEST['operator'] = $G_PGLOB->currentUserID; //创建人
        $result_array = $mapAreaZoning->editMapArea( $_REQUEST ) ;

        if ($result_array['code'] == 200) {
            returnJsonData( $result_array['message'], $result_array['code'], $result_array['data'] );
        } else {
            returnJsonData( $result_array['message'], $result_array['code'] );
        }

    }


    /*
     *  根据区域信息查询所有的区域划分信息
     */
    private function searchMapArea(){

        if(!isset($_REQUEST['cityID']) || !quanli_is_int($_REQUEST['cityID'])){
            returnJsonData('参数错误');
        }

        //实例化地图区域划分类
        $mapAreaZoning = new mapAreaZoning();

        //参数组装
        $arg_array = array(
            'cityID' => $_REQUEST['cityID']
        );
        $map_area_array = $mapAreaZoning->searchMapArea( $arg_array );
        if (!$map_area_array) {
            returnJsonData('没有地图区域', 404);
        }

        $return_map_area_array = array();

        foreach($map_area_array as $map_area) {

            //查询区域坐标详情
            $map_area_points_array = $mapAreaZoning->searchMapAreaPath( $map_area['areaID'] );
            $map_area['path'] = $map_area_points_array;


            $map_area_brand_array = $mapAreaZoning->searchMapAreaBrand($map_area['areaID'],$map_area['cityID'],$map_area['type']);
            $map_area['brand'] = $map_area_brand_array;

            $return_map_area_array[$map_area['type']][$map_area['areaID']] = $map_area;

        }

        returnJsonData('ok', 200, $return_map_area_array );

    }

    /**
     * 删除地图区域
     */
    private function delMapArea() {

        if (!isset($_REQUEST['areaID']) || !quanli_is_int($_REQUEST['areaID'])) {
            returnJsonData('参数错误');
        }

        //实例化地图区域划分类
        $mapAreaZoning = new mapAreaZoning();

        //删除地图区域
        $result_array = $mapAreaZoning->delMapArea( $_REQUEST['areaID'] );

        returnJsonData( $result_array['message'], $result_array['code'] );

    }

}
